package com.example.cnotebook;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.cnotebook.pojo.CategoryInfo;
import com.example.cnotebook.pojo.NoteInfo;
import com.example.cnotebook.util.OKHttpUtil;
import com.example.cnotebook.util.UserConstant;

import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AddNoteActivity extends BaseActivity implements View.OnClickListener, OKHttpUtil.CallBack {
    private Spinner spCategory;
    private ImageView imSave;
    private EditText etTitle;
    private EditText etContent;
    private ImageView ivBack;

    private ArrayList<String> categorys = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private int noteId = -1;

    private String addCategoryUrl = "/note/add";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_note);
        noteId = getIntent().getIntExtra("noteId", -1);
        initView();
        initData();
        if (noteId != -1) {
            ArrayList<NoteInfo> noteInfos = UserConstant.noteInfos;
            Integer categoryId = -1;
            for (NoteInfo n : noteInfos) {
                if (n.getId() == noteId) {
                    etTitle.setText(n.getTitle());
                    etContent.setText(n.getContent());
                    categoryId = n.getCategoryId();
                }
            }
            ArrayList<CategoryInfo> categoryInfos = UserConstant.categoryInfos;
            for (int i = 0; i < categoryInfos.size(); i++) {
                CategoryInfo categoryInfo = categoryInfos.get(i);
                if (categoryInfo.getId().equals(categoryId)) {
                    spCategory.setSelection(i + 1);
                }
            }
        }
    }

    private void initView() {
        categorys.add("未选择");
        spCategory = findViewById(R.id.sp_category);
        imSave = findViewById(R.id.im_save);
        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        ivBack = findViewById(R.id.iv_back);

        adapter = new ArrayAdapter<>(this, R.layout.spinner_item, categorys);
        spCategory.setAdapter(adapter);
        spCategory.setSelection(0);

        imSave.setOnClickListener(this);
        ivBack.setOnClickListener(this);
    }

    private void initData() {
        for (CategoryInfo categoryInfo : UserConstant.categoryInfos) {
            categorys.add(categoryInfo.getName());
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.im_save) {
            try {
                if (noteId == -1) {
                    saveData();
                } else {
                    updateData();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    private void updateData() throws JSONException {
        JSONObject jsonObject = getRequestData();
        if (jsonObject == null) {
            return;
        }
        jsonObject.put("id",noteId);
        OKHttpUtil.post("/note/update", jsonObject, this, this);
    }

    private void saveData() throws JSONException {
        JSONObject jsonObject = getRequestData();
        if (jsonObject == null) return;
        OKHttpUtil.post(addCategoryUrl, jsonObject, this, this);
    }

    @Nullable
    private JSONObject getRequestData() throws JSONException {
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        if (title.equals("") && content.equals("")) {
            Toast.makeText(this, "标题或者内容不能为空！", Toast.LENGTH_SHORT).show();
            return null;
        }
        int position = spCategory.getSelectedItemPosition();
        String s = categorys.get(position);
        int id = -1;
        for (CategoryInfo categoryInfo : UserConstant.categoryInfos) {
            if (categoryInfo.getName().equals(s)) id = categoryInfo.getId();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("title", title);
        jsonObject.put("content", content);
        jsonObject.put("hasAudio", 0);
        jsonObject.put("categoryId", id);
        jsonObject.put("userId", UserConstant.userInfo.getId());
        return jsonObject;
    }

    @Override
    public void onSuccess(JSONObject response) throws JSONException {
        if (noteId == -1){
            Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "修改成功!", Toast.LENGTH_SHORT).show();
        }
        //用于确定是否更新fragment
        UserConstant.userIsSaveData = true;
        UserConstant.homeIsSaveData = true;
        finish();
    }

    @Override
    public void onFailure(int errorNo, String strMsg) {

    }
}
